include_javaScript(url_prefix + 'editor/ckeditor/ckeditor.js');
include_javaScript(url_prefix + 'editor/ckfinder/ckfinder.js');


$(function () {
    $("#tabs").tabs();
});

$(document).ready(function () {

    hs.graphicsDir = url_base + 'app/admin/view/temp/default/layout/css/highslide/graphics/';
    hs.align = 'center';
    hs.closeText = '<img border="0" src="' + url_base + 'app/admin/view/temp/default/layout/css/highslide/graphics/close_X.png" />';
    hs.creditsText = 'this.a.title';
    hs.outlineType = 'rounded-white';
    hs.showCredits = false;
    hs.wrapperClassName = 'draggable-header';
    hs.cacheAjax = false;
    hs.dimmingOpacity = 0.7;
    hs.onDimmerClick = function () {
        return false;
    }


    $('#btn-submit').click(function () {

        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailaddressVal = $("#Email").val();
        if (emailaddressVal == '') {
            $(".error").show().text(_['error-emal-empty']);
            hasError = true;
        }

        else if (!emailReg.test(emailaddressVal)) {
            $(".error").show().text(_['error-emal-no-format']);
            hasError = true;
        }

        if (hasError == true) {
            return false;
        }

    });


});
function move_div(ID, NO) {
    $("#div_sort").sortable({
        start:function (event, ui) {
            var order_start = $('#div_sort').sortable('toArray');

        },
        stop:function (event, ui) {
            if (confirm(_['error_order'])) {
                var order_stop = $('#div_sort').sortable('toArray');

                var count = order_stop.length;
                var order_sort = "";
                for (i = 0; i <= count; i++) {
                    if (order_stop[i] != undefined) {
                        var id = order_stop[i].split("_");
                        order_sort += "," + id[1];
                    }
                }
                var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/order/" + order_sort;
                $.ajax({
                    type:"POST",
                    url:url_base + "" + strData,
                    cache:false,
                    success:function (DataHtml) {
                        if (DataHtml == "ok") {
                            for (i = 0; i <= count; i++) {
                                if (order_stop[i] != undefined) {
                                    var id = order_stop[i].split("_");
                                    $("#Order" + id[1]).html(i + 1);
                                }

                            }
                        }

                    }
                });
            }
            else
                $('#div_sort').sortable('cancel');
        }

    });

}
function enble_btn() {
    $('input:checkbox[name="delId[]"]').click(function () {

        if ($(this).attr("checked") == "checked") {

            $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled", false);

        } else {

            if (getValCb().length > 0) {
                $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled", false);
            } else {
                $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled", true);
            }


        }
    });
}
function checkAll() {
    if (getValCb().length > 0) {

        $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled", true);

        $('input:checkbox[name="all"]').removeAttr("checked");
        $('input:checkbox[name="delId[]"]').removeAttr("checked");

    } else {

        $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled", false);

        $('input:checkbox[name="all"]').attr("checked", "checked");
        $('input:checkbox[name="delId[]"]').attr("checked", "checked");
    }

}

function getValCb() {
    var delId = [];
    $('input:checkbox[name="delId[]"]:checked')
        .each(function (index) {
            delId.push($(this).val());
        });

    return delId;
}


function getHtml(active) {
//    if(StrID=="")
//    var StrID = getValCb();

    var check = false;
    switch (active) {
        case 'add':
            var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/" + active;
            check = true;
            break;
        case 'edit':
            $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled", true);
            var StrID = getValCb();
            var value_arr = new Array();
            var value_control = new Array();
            $('input:checkbox[name="delId[]"]:checked')
                .each(function (index) {
                    var fullnam = $("#Fullname" + $(this).val()).html();
                    var nick = $("#Nick" + $(this).val()).html();
                    var kind = $("#Kind" + $(this).val()).html();
                    var mobile = $("#Mobile" + $(this).val()).html();
                    var order = $("#Order" + $(this).val()).html();
                    var status = $("#Status" + $(this).val()).val() == true ? "true" : "false";
                    value_control = [fullnam, nick, kind, mobile, order, status];
                    value_arr[$(this).val()] = value_control;
                });
            if (StrID != "")
                check = true;
            var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/" + active + "/" + StrID;
            break;
    }
    if (check) {
        $.ajax({
            type:"POST",
            url:url_base + "" + strData,
            cache:false,
            dataType:"html",
            success:function (DataHtml) {
                switch (active) {
                    case 'add':
                        if ($("#tr_new").length == 0)
                            $("#tbl_header").after(DataHtml);
                        break;
                    case 'edit':
                        $('input:checkbox[name="delId[]"]:checked')
                            .each(function (index) {
                                var str = "tr_" + $(this).val();
                                var arr = JSON.parse(DataHtml)
                                if ($("." + str).length != 0 && arr[$(this).val()] != "") {
                                    $("." + str).html(arr[$(this).val()]);
                                    $("#Fullname_edit" + $(this).val()).attr("old", value_arr[+$(this).val()][0]);
                                    $("#Nick_edit" + $(this).val()).attr("old", value_arr[$(this).val()][1]);
                                    $("#Kind_edit" + $(this).val()).attr("old", value_arr[$(this).val()][2]);
                                    $("#Mobile_edit" + $(this).val()).attr("old", value_arr[$(this).val()][3]);
                                    $("#Order_edit" + $(this).val()).attr("old", value_arr[$(this).val()][4]);
                                    $("#Status_edit" + $(this).val()).attr("old", value_arr[$(this).val()][5]);

                                    $("#Fullname_edit" + $(this).val()).val(value_arr[$(this).val()][0]);
                                    $("#Nick_edit" + $(this).val()).val(value_arr[$(this).val()][1]);
                                    $("#Kind_edit" + $(this).val()).val(value_arr[$(this).val()][2]);
                                    $("#Mobile_edit" + $(this).val()).val(value_arr[$(this).val()][3]);
                                    $("#Order_edit" + $(this).val()).val(value_arr[$(this).val()][4]);
                                    $("#Status_edit" + $(this).val()).val(value_arr[$(this).val()][5]);
                                }
                            });

                        break;
                }
            }
        });
    }
    else
        alert(_['error_choise']);

}

function deltr(action, ID) {
    var checkbox = '<input name=\"delId[]\" type=\"checkbox\" style=\"width: 96px\" value="' + ID + '">';
    if (action == "add") {
        $("#tr_new").remove();
        if ($("#erorr").length > 0)
            $("#erorr").remove();
    }

    if (action == "edit") {
        var value_f = $("#Fullname_edit" + ID).attr("old");
        var value_n = $("#Nick_edit" + ID).attr("old");
        var value_k = $("#Kind_edit" + ID).attr("old");
        var value_m = $("#Mobile_edit" + ID).attr("old");
        var value_o = $("#Order_edit" + ID).attr("old");
        var value_s = $("#Status_edit" + ID).attr("old");
        $("#Fullname" + ID).html(value_f);
        $("#Nick" + ID).html(value_n);
        $("#Kind" + ID).html(value_k);
        $("#Mobile" + ID).html(value_m);
        $("#Order" + ID).html(value_o);
        $("#Status" + ID).html(value_s == true ? _['true'] : _['false']);
        $("#checkbox" + ID).html(checkbox);

        $("#Order" + ID).attr("onmousedown", 'move_div("' + ID + '")');
        if ($("#erorr").length > 0)
            $("#erorr").remove();
        enble_btn();
    }

}
function save_data(action, ID) {
    var error = "border: 1px solid red;";
    if (action == "edit") {
        var value_f = $("#Fullname_edit" + ID).val();
        var value_n = $("#Nick_edit" + ID).val();
        var value_k = $("#Kind_edit" + ID).val();
        var value_m = $("#Mobile_edit" + ID).val();
        var value_o = $("#Order_edit" + ID).val();
        var value_s = $("#Status_edit" + ID).val();
    }
    else {
        var value_f = $("#Fullname_new").val();
        var value_n = $("#Nick_new").val();
        var value_k = $("#Kind_new").val();
        var value_m = $("#Mobile_new").val();
        var value_o = $("#Order_new").val();
        var value_s = $("#Status_new").val();
    }
    var data_ajax = {
        Fullname:value_f,
        nick:value_n,
        Kind:value_k,
        Order:value_o,
        Mobile:value_m,
        Status:value_s
    };
    if (action == "add")
        var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/add_data";
    if (action == "edit")
        var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/edit_data/" + ID;
    $.ajax({
        type:"POST",
        url:url_base + "" + strData,
        data:data_ajax,
        cache:false,
        success:function (DataHtml) {
            if (action == "add" && DataHtml != "NotOk") {
                $("#tr_new").after(DataHtml);
                if ($("#erorr").length > 0)
                    $("#erorr").remove();
                enble_btn();
            }
            else if (action == "add" && DataHtml == "NotOk") {
                if ($("#erorr").length == 0)
                    $("#tbl_header").before('<div style="color: red" id="erorr">' + _['error_save'] + '</div>');

            }
            if (action == "edit" && DataHtml != "NotOk") {
                $("#tr_" + ID).html(DataHtml);
                if ($("#erorr").length > 0)
                    $("#erorr").remove();
                enble_btn();
            }
            else if (action == "edit" && DataHtml == "NotOk") {
                if ($("#erorr").length == 0)
                    $("#tbl_header").before('<div style="color: red" id="erorr">' + _['error_save'] + '</div>');

            }
        }
    });
}
function del_data() {
    var StrID = getValCb();
    var strData = _seokey[0] + "/" + _seokey[1] + "/ajax/delete/" + StrID;
    if (confirm(_['delete_contact'])) {
        $.ajax({
            type:"POST",
            url:url_base + "" + strData,
            cache:false,
            success:function (DataHtml) {
                var url = url_base + _seokey[0] + "/" + _seokey[1];
                $(location).attr('href', url);
            }
        });
    }

}









